package com.shfl.servermanage.logic.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.shfl.servermanage.logic.bean.SysSiteConfig;
import com.shfl.servermanage.logic.constants.CommonConstants;
import com.shfl.servermanage.logic.mapper.SysSiteConfigMapper;
import com.shfl.servermanage.logic.model.info.AddSingleAttachmentInfo;
import com.shfl.servermanage.logic.model.request.AddSiteConfigRequest;
import com.shfl.servermanage.logic.model.response.SiteConfigPageResponse;
import com.shfl.servermanage.logic.service.SysAttachmentService;
import com.shfl.servermanage.logic.service.SysSiteConfigService;
import com.shfl.servermanage.pagehelper.PageHelperUtils;
import com.shfl.servermanage.pagehelper.Pageable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class SysSiteConfigServiceImpl implements SysSiteConfigService {
    @Autowired
    private SysSiteConfigMapper sysSiteConfigMapper;

    @Autowired
    private SysAttachmentService sysAttachmentService;

    @Override
    public PageInfo<SiteConfigPageResponse> findPage(Pageable pageable, Map<String, Object> objectMap) {
        PageHelperUtils.startPageWithCount(pageable);
        return new PageInfo<>(sysSiteConfigMapper.findPage(objectMap));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddSiteConfigRequest addSiteConfigRequest) {
        SysSiteConfig sysSiteConfig = new SysSiteConfig();
        BeanUtil.copyProperties(addSiteConfigRequest, sysSiteConfig);
        sysSiteConfigMapper.insertSelective(sysSiteConfig);
        sysAttachmentService.addSingle(new AddSingleAttachmentInfo(
                addSiteConfigRequest.getFileId(),
                sysSiteConfig.getId(),
                CommonConstants.AttachType.SITE_CONFIG.getValue()
        ));
    }

    @Override
    public List<SiteConfigPageResponse> findList(Map<String, Object> objectMap) {
        return sysSiteConfigMapper.findList(objectMap);
    }
}
